import 'package:distribution_mall_flutter/assets.dart';
import 'package:distribution_mall_flutter/constant/cf_constant.dart';
import 'package:distribution_mall_flutter/constant/constant.dart';
import 'package:distribution_mall_flutter/constant/point_constant.dart';
import 'package:distribution_mall_flutter/constant/status_constant.dart';
import 'package:distribution_mall_flutter/entity/order/list/order_list_entity.dart';
import 'package:distribution_mall_flutter/entity/order/order.dart';
import 'package:distribution_mall_flutter/generated/l10n.dart';
import 'package:distribution_mall_flutter/pages/order/order_list/widgets/content_card_footer.dart';
import 'package:distribution_mall_flutter/provider/order/order_list.dart';
import 'package:distribution_mall_flutter/utils/firebase/firebase_analytics_utils.dart';
import 'package:distribution_mall_flutter/utils/image_loader.dart';
import 'package:distribution_mall_flutter/router/router.gr.dart';
import 'package:distribution_mall_flutter/utils/utils.dart';
import 'package:distribution_mall_flutter/widgets/card.dart';
import 'package:distribution_mall_flutter/widgets/goods_item_tile.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:skeletonizer/skeletonizer.dart';
import 'package:slide_countdown/slide_countdown.dart';

class ContentCard extends ConsumerWidget {
  final Item data;
  const ContentCard({super.key, required this.data});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    List<OrderTransportEntity> list =
        ref.watch(orderListProvider.select((value) => value.transportList));
    OrderSuItemList? orderSuItemList =
        data.orderItemGroupList?[0].orderSuItemList?[0];
    String statusText = (data.paymentState == OrderStatusEnum.toPay.status &&
            data.state == OrderStatusEnum.waitAudit.status)
        ? OrderStatusEnum.toPay.statusText
        : OrderStatusEnum.getStatusText(data.state);
    return EPCard(
      onPressed: () {
        appRouter.push(OrderDetailRoute(
            distributionOrderId: data.distributionOrderId ?? ''));
        FirebaseAnalyticsUtils.logEvent(
            name: PointConstant.orderItem,
            parameters: {
              'orderState': statusText,
              'distributionOrderId': data.distributionOrderId ?? ''
            });
      },
      enableClickEffect: true,
      margin: const EdgeInsets.fromLTRB(
          EdgeConstant.edge10, EdgeConstant.edge10, EdgeConstant.edge10, 0),
      padding: const EdgeInsets.symmetric(
          vertical: EdgeConstant.edge15, horizontal: EdgeConstant.edge10),
      footerChild: Positioned(
          left: EdgeConstant.edge10,
          bottom: EdgeConstant.edge10,
          right: EdgeConstant.edge10,
          child: ContentCardFooter(
            data: data,
          )),
      child: Stack(
        children: [
          Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  if (data.state == OrderStatusEnum.toPay.status)
                    SlideCountdownSeparated(
                      onDone: () {
                        if (data.paymentDeadline != null &&
                            data.paymentDeadline! >
                                (DateTime.now().millisecondsSinceEpoch -
                                    3000)) {
                          ref
                              .read(orderListProvider.notifier)
                              .refresh({}, false);
                        }
                      },
                      // separatorStyle: const TextStyle(
                      //     color: Color.fromRGBO(255, 0, 0, 1),
                      //     fontWeight: FontWeight.bold),
                      padding: const EdgeInsets.symmetric(
                          horizontal: 5, vertical: 2),
                      duration: Duration(
                          seconds: DateTime.fromMillisecondsSinceEpoch(
                                  data.paymentDeadline ??
                                      DateTime.now().millisecondsSinceEpoch)
                              .difference(DateTime.now())
                              .inSeconds),
                      decoration: const BoxDecoration(
                        borderRadius: BorderRadius.all(Radius.circular(25)),
                        color: Color(0xFFF23333),
                      ),
                      style: const TextStyle(
                          color: Colors.white,
                          fontSize: 14,
                          letterSpacing: TextConstant.letterSpacing),
                    ),
                  const SizedBox(
                    width: 10,
                  ),
                  Text(
                    statusText,
                    style: const TextStyle(
                        color: Colors.black,
                        fontWeight: FontWeight.bold,
                        fontSize: FontConstant.f11),
                  )
                ],
              ),
              Padding(
                padding:
                    const EdgeInsets.symmetric(vertical: EdgeConstant.edge10),
                child: EPGoodsItemTile(
                    otherChild: data.orderType == OrderTypeConstant.sample
                        ? Image.asset(Assets.assets_icons_order_sample_png,
                            width: 50, height: 16)
                        : null,
                    data: EPGoodsItemTileransform.fromJson({
                      'mainImage': orderSuItemList?.mainImage,
                      'title': orderSuItemList?.title,
                    })),
              ),
              DefaultTextStyle(
                  style: const TextStyle(
                      color: ColorConstant.grey136, fontSize: FontConstant.f11),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                              '${S.of(context).items}: x${data.orderItemGroupList?[0].orderSuItemList?[0].orderGoodsList?.length ?? '-'}'),
                          Text(ref
                              .watch(orderListProvider.notifier)
                              .getTransportName(data.transportType))
                        ],
                      ),
                      Utils.currencyWidget(data.totalAmount,
                          label: '${S.of(context).total}: ',
                          labelStyle: const TextStyle(
                              fontSize: FontConstant.f12, color: Colors.black),
                          symbolStyle: const TextStyle(
                              fontSize: FontConstant.f12, color: Colors.black),
                          priceStyle: const TextStyle(
                              fontWeight: FontWeight.bold,
                              color: Colors.black,
                              fontSize: FontConstant.f17)),
                    ],
                  )),
              const SizedBox(height: 40)
            ],
          ),
        ],
      ),
    );
  }
}
